package com.itheima.web;

import com.itheima.domain.Brand;
import com.itheima.service.BrandService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.annotations.Insert;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/*
    品牌信息的 表现层！
 */
@WebServlet(urlPatterns = "/BrandServlet")
public class BrandServlet extends HttpServlet {

    private BrandService service = new BrandService();

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 处理响应中文乱码    
        response.setContentType("text/html;charset=utf-8");

        // 获得请求参数action的值
        String action = request.getParameter("action"); // 发送请求时，通过相同参数的不同值来区分用户到底进行的是哪一个功能！
        // 判断
        if ("findAll".equals(action)) {
            // 查询所有
            findAll(request, response);
        } else if ("addBrand".equals(action)) {
            // 添加
            addBrand(request,response);
        } else if ("findById".equals(action)) {
            // 查询指定记录
            findById(request,response);
        }else if ("updateBrand".equals(action)) {
            // 修改指定记录
            updateBrand(request,response);
        }else if ("deleteById".equals(action)) {
            // 删除指定记录
            deleteById(request,response);
        }

        /**
         *  使用反射优化这块代码！
         */

    }

    /**
     *  删除指定品牌信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 1. 获得请求参数id
        String id = request.getParameter("id");
        // 2.调用service层的方法
        service.deleteById(Integer.parseInt(id));
        // 3. 重定向到查询所有
        response.sendRedirect("/day28_brand/BrandServlet?action=findAll");
    }

    /**
     *  修改指定品牌信息
     *
      * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void updateBrand(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 获得修改品牌信息提交的表单所有数据
        Map<String, String[]> map = request.getParameterMap();
        // 将数据封装到一个Brand对象
        Brand brand = new Brand();
        // 借助工具BeanUtils将map数据封装到brand对象！
        try {
            BeanUtils.populate(brand,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 调用service层修改的方法
        service.updateBrand(brand);
        // 重定向到查询所有的Servlet
        response.sendRedirect("/day28_brand/BrandServlet?action=findAll");

    }

    /**
     *  查询指定品牌信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 获得参数id
        String id = request.getParameter("id");
        // 调用service层查询的方法
        Brand brand = service.findById(Integer.parseInt(id));
        // 将查询结果保存request域中
        request.setAttribute("brand",brand);
        // 转发到brand_update.jsp页面
        request.getRequestDispatcher("brand_update.jsp").forward(request,response);
    }

    /**
     *  添加品牌信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void addBrand(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 获得添加品牌信息提交的表单所有数据
        Map<String, String[]> map = request.getParameterMap();
        // 将数据封装到一个Brand对象
        Brand brand = new Brand();
        // 借助工具BeanUtils将map数据封装到brand对象！
        try {
            BeanUtils.populate(brand,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // System.out.println(brand);

        // 调用service层添加品牌信息的方法
        service.addBrand(brand);

        // 重定向到查询所有的Servlet
        response.sendRedirect("/day28_brand/BrandServlet?action=findAll");


    }

    /**
     *  查询所有
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 1. 调用service层查询所有的方法
        List<Brand> brands = service.findAll();
        // 2. 将查询结果保存request域中
        request.setAttribute("brands", brands);
        // 3. 转发到列表页面
        request.getRequestDispatcher("brand_list.jsp").forward(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // 处理请求的中文乱码    
        request.setCharacterEncoding("utf-8");
        doGet(request, response);
    }

}

